using System;
using System.Linq;
using System.Windows;
using System.Windows.Input;
using Microsoft.Win32;

namespace Exones.Blend.AssetsManager
{
	class BlendAssetFolder: BlendFolder
	{
		private string path;
		private ICommand deleteAssetCommand;
		private ICommand editAssetCommand;



		public ICommand EditAssetCommand
		{
			get { return editAssetCommand; }
		}

		public ICommand DeleteAssetCommand
		{
			get { return deleteAssetCommand; }
		}

		public override bool IsLeaf
		{
			get { return true; }
		}

		public string Path
		{
			get { return path; }
			set
			{
				path = value;
				NotifyOfPropertyChange(() => Path);
			}
		}

		public void DeleteAsset()
		{
			if (MessageBox.Show(Application.Current.MainWindow, "Delete this asset?", "Deleting asset", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
			{
				return;
			}

			try
			{
				var key = Registry.LocalMachine.OpenSubKey(ParentFolder.RegistryPath, true);

				if (key == null)
				{
					return;
				}

				key.DeleteSubKeyTree(this.Name);
				ParentFolder.SubFolders.Remove(this);
				
			}
			catch (Exception exc)
			{
				MessageBox.Show(Application.Current.MainWindow,
				                string.Format("Couldn't delete asset. Try running application as Administrator.\n\nError:\n{0}",
				                              exc.Message), "Error deleting asset", MessageBoxButton.OK, MessageBoxImage.Error);
			}
			
		}

		public void EditAsset()
		{
			this.Name += ": edited";
		}

		public BlendAssetFolder(string name, string path): base(name, Enumerable.Empty<BlendFolder>())
		{
			if (path == null) throw new ArgumentNullException("path");

			this.path = path;
			IsSelectable = false;
			deleteAssetCommand = new DelegateCommand(DeleteAsset, () => true);
			editAssetCommand = new DelegateCommand(EditAsset, () => true);
		}
	}
}